package com.letter.otools.day.todo;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.textfield.TextInputEditText;
import com.letter.otools.day.BaseActivity;
import com.letter.otools.day.R;
import com.letter.otools.day.util.StringUtils;

public class TodoAddActivity extends BaseActivity {
    private MaterialToolbar toolbar;
    private TextInputEditText etTitle;
    private TextInputEditText etContent;

    @Override
    protected int getLayout() {
        return R.layout.todo_edit;
    }

    @Override
    protected void initView() {
        setSupportToolbarBack(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_todo_edit, menu);
        return true;
    }

    @Override
    public void initBinder() {
        toolbar = findViewById(R.id.toolbar);
        etTitle = findViewById(R.id.etTitle);
        etContent = findViewById(R.id.etContent);
    }

    private void saveTodo() {
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        if (StringUtils.isEmpty(title) && StringUtils.isEmpty(content)) {
            Toast.makeText(TodoAddActivity.this, "The title and content cannot both be empty.", Toast.LENGTH_SHORT).show();
            return;
        }
        Todo Todo = new Todo();
        Todo.setTitle(title);
        Todo.setContent(content);
        Todo.setCreateTime(System.currentTimeMillis());
        Todo.setUpdateTime(Todo.getCreateTime());
        Todo.setDoTime(Todo.getCreateTime());
        Todo.setDoDay(TimeUtils.millis2String(Todo.getCreateTime(), "yyyy-MM-dd"));
        Todo.setStatus(0);
        Todo.setMood(1);
        Todo.setType(1);
        Todo.save();
        etTitle.setText("");
        etContent.setText("");
        Toast.makeText(TodoAddActivity.this, "Saved successfully", Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_save) {
            saveTodo();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
